package org.carabiner.example;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Point;
import java.awt.Rectangle;
import java.awt.Shape;
import java.awt.event.MouseEvent;

import javax.swing.JPanel;

import org.carabiner.mockutil.DynamicStub;
import org.jmock.Mock;
import org.jmock.MockObjectTestCase;

public class TestSquareMouseController extends MockObjectTestCase
{
  public void testDrawRect()
  {
    Point location = new Point(20, 30);
    Dimension size = new Dimension(40, 100);
    final Rectangle expectedRect = new Rectangle(location, size);
    Mock mockModel = new Mock(ShapeModel.class);
    mockModel.expects(atLeastOnce()).will(new DynamicStub()
    {    
      public void addShape(Shape shape, Color color)
      {
        assertEquals(expectedRect, shape);
        assertEquals(Color.RED, color);
      }
    });
    JPanel source = new JPanel();
    SquareMouseController controller = new SquareMouseController((ShapeModel)mockModel.proxy());    
    source.addMouseListener(controller);    
    MouseEvent e = createMouseEvent(location, source, MouseEvent.MOUSE_PRESSED);
    controller.mousePressed(e);
    assertEquals("Rect location was not set", location, controller.getRect().getLocation());

    location.translate(size.width, size.height);
    e = createMouseEvent(location, source, MouseEvent.MOUSE_RELEASED);    
    controller.mouseReleased(e);
    assertEquals("Rect width/height was not set", size, controller.getRect().getSize());
    assertEquals(0, source.getMouseListeners().length);
    mockModel.verify();
  }

  private MouseEvent createMouseEvent(Point location, JPanel source, int type)
  {
    MouseEvent e = new MouseEvent(source, type, System.currentTimeMillis(), 0, location.x, location.y, 1, false);
    return e;
  }
  
}
